package cn.miao.learn.ssh.controllers;


import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import cn.miao.learn.ssh.beans.TGroup;
import cn.miao.learn.ssh.beans.TPerson;
import cn.miao.learn.ssh.beans.TUser;
import cn.miao.learn.ssh.services.TestHibernateService;
import cn.miao.learn.ssh.sqlmapper.TUserMapper;

import com.opensymphony.xwork2.ActionSupport;


/**
 * 此处的action由spring去负责实例化，默认是单例的，
 * 为迎合struts的action，现设置器scope为protoype
 */
@Service(value="testStrutsAction")
@Scope("prototype")
public class TestStrutsAction extends ActionSupport {
	private static final long serialVersionUID = 3362122496775495186L;
	
	@Resource(name="TestHibernate")
	private TestHibernateService testHibernateService;
	
	@Resource(name="TestMyBatis")
	private TUserMapper tUserMapper;
	
	private Map<String, Object> jsonMap;
	private List<TUser> userList;
	private String str;
	/**
	 * 该action为xml方式配置的，此处测试其方法的调用，
	 * 用Hibernate执行了查询，此处的映射配置基于xml 
	 */
	public String testCall() {
		List<TPerson> list = testHibernateService.getTpersonList();
		for(TPerson p : list) {
			System.out.println(p.getName());
		}
		
		return SUCCESS;
	}
	
	/**
	 * 用Hibernate执行了查询，此处的映射配置基于annotation 
	 */
	public String testAnnotationCall() {
		List<TGroup> list = testHibernateService.getTGroupList();
		
		for(TGroup g : list) {
			System.out.println(g.getName());
		}
		
		return NONE;
	}
	
	
	/**
	 * 测试json interceptor是否起作用
	 */
	public String testJsonCall() {
		jsonMap = new HashMap<String, Object>();
		jsonMap.put("a", "aa");
		jsonMap.put("b", "中午");
		jsonMap.put("c", new Date());
		return "json";
	}
	
	/**
	 * 测试MyBatis 整合
	 */
	public String testMybatisCall() {
		TUser user = new TUser();
		user.setId(1);
		user.setName("oham");
		userList = tUserMapper.selectTUserByExample(user);
		str= "测试";
		return SUCCESS;
	}

	public Map<String, Object> getJsonMap() {
		return jsonMap;
	}

	public List<TUser> getUserList() {
		return userList;
	}

	public void setUserList(List<TUser> userList) {
		this.userList = userList;
	}

	public String getStr() {
		return str;
	}

	public void setStr(String str) {
		this.str = str;
	}
}